import * as Blockly from 'blockly';

// 异步日志块定义
export const asyncBlocks = {
    asyncLog: {
        type: 'async_log',
        message0: '异步输出日志 %1',
        args0: [
            {
                type: 'input_value',
                name: 'TEXT',
                check: null, // 允许任何类型的输入
            },
        ],
        previousStatement: null,
        nextStatement: null,
        colour: 160,
        tooltip: '异步输出内容到控制台',
        helpUrl: '',
    }
};

// 注册异步块
let asyncLogBlocksRegistered = false;

// 注册异步块
export function registerAsyncLogBlocks() {
    // 如果已经注册过异步块，则直接返回
    if (asyncLogBlocksRegistered) {
        console.log('Async log blocks already registered, skipping registration');
        return;
    }
    
    try {
        // 检查块是否已经存在
        if (Blockly.Blocks['async_log']) {
            console.log('Block async_log already exists, skipping definition');
        } else {
            // 注册异步块
            Blockly.common.defineBlocks(
                Blockly.common.createBlockDefinitionsFromJsonArray([
                    asyncBlocks.asyncLog
                ])
            );
            console.log('Async log blocks registered successfully');
        }
        
        // 标记已经注册
        asyncLogBlocksRegistered = true;
    } catch (error) {
        console.error('Error registering async log blocks:', error);
    }
}